package com.longway.core.ext.render.excel;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.CellStyle;

import com.jfinal.kit.LogKit;
import com.jfinal.render.Render;
import com.jfinal.render.RenderException;
import com.longway.core.ext.excel.data.TableDataset;
import com.longway.core.ext.excel.util.ExcelKit;
import com.longway.core.ext.excel.util.TemplateExcelKit;
import com.longway.core.ext.kit.excel.PoiExporter;

public class PoiRender extends Render {

    private final static String CONTENT_TYPE = "application/msexcel;charset=" + getEncoding();
    private List<TableDataset> data;
    private String[][] headers;
    private String[] sheetNames = new String[]{};
    private int cellWidth;
    private String[] columns = new String[]{};
    private String fileName = "file1.xls";
    private int headerRow;
    private String version;
    private Map<Integer, Integer> colwidths = null;
    private String[] titles = null;
    private Map<String,CellStyle>[] cellstyles = null;
    private export outtype = export.CONTENT;
    private String templateFile = null;
    
    public static enum  export{
        CONTENT,TEMPLATE
   }
    
    public PoiRender(List<TableDataset> data) {
        this.data = data;
    }

    public static PoiRender me(List<TableDataset> data) {
        return new PoiRender(data);
    }

    @Override
    public void render() {
    	switch (outtype) {
			case CONTENT:
				contentRender();
				break;
			case TEMPLATE:
				templateRender();
				break;
			default:
				break;
		 }
    }
    
    private void contentRender(){
    	response.reset();
        try {
    		response.setHeader("Pragma", "public");
    	    response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
			response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1" ));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        response.setContentType(CONTENT_TYPE);
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            ExcelKit.data(data).version(version).sheetNames(sheetNames).headerRow(headerRow).headers(headers).columns(columns)
                    .cellWidth(cellWidth).colwidths(colwidths).cellstyles(cellstyles).export().write(os);
        } catch (Exception e) {
            throw new RenderException(e);
        } finally {
            try {
                if (os != null) {
                    os.flush();
                    os.close();
                }
            } catch (IOException e) {
                LogKit.error(e.getMessage(), e);
            }
        }
    }
    
    private void templateRender(){
    	response.reset();
        try {
			response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1" ));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        response.setContentType(CONTENT_TYPE);
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            TemplateExcelKit.data(data).version(version).templateFile(templateFile).sheetNames(sheetNames).headerRow(headerRow)
                    .cellstyles(cellstyles).export().write(os);
        } catch (Exception e) {
            throw new RenderException(e);
        } finally {
            try {
                if (os != null) {
                    os.flush();
                    os.close();
                }
            } catch (IOException e) {
                LogKit.error(e.getMessage(), e);
            }
        }
    }

    public PoiRender headers(String[]... headers) {
        this.headers = headers;
        return this;
    }

    public PoiRender headerRow(int headerRow) {
        this.headerRow = headerRow;
        return this;
    }

    public PoiRender columns(String... columns) {
        this.columns = columns;
        return this;
    }

    public PoiRender sheetName(String... sheetName) {
        this.sheetNames = sheetName;
        return this;
    }

    public PoiRender cellWidth(int cellWidth) {
        this.cellWidth = cellWidth;
        return this;
    }

    public PoiRender fileName(String fileName) {
        this.fileName = fileName;
        return this;
    }

    public PoiRender version(String version) {
        this.version = version;
        return this;
    }

	public PoiRender colwidths(Map<Integer, Integer> colwidths) {
		this.colwidths = colwidths;
		return this;
	}
	
	public PoiRender titles(String[] titles) {
		this.titles = titles;
		return this;
	}
    
	public PoiRender outtype(export outtype){
		this.outtype = outtype;
		return this;
	}
	
	public PoiRender templateFile(String templateFile){
		this.templateFile = templateFile;
		return this;
	}
}